﻿using SkiaSharp;
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using TabbedTemplate.Models;
using TabbedTemplate.Services;
using TabbedTemplate.Views;
using ImageSource = System.IO.MemoryStream;

namespace TabbedTemplate.Droid
{
    public class DrawPicService : IDrawPicService
    {
        private ITextAnalysisService _textAnalysisService;
        public DrawPicService(ITextAnalysisService textAnalysisService)
        {

            _textAnalysisService = textAnalysisService;
        }
        public void DrawPic()
        {


            //Bitmap bitmap = Bitmap.CreateBitmap(100, 100, Bitmap.Config.Argb8888);
            //Canvas canvas = new Canvas(bitmap);
            //Paint green = new Paint
            //{
            //    AntiAlias = true,
            //    Color = Color.Rgb(0x99, 0xcc, 0),
            //};
            //green.SetStyle(Paint.Style.FillAndStroke);
            //canvas.DrawCircle(1,1,1,green);
            //SKCanvasView canvasView = new SKCanvasView();


        }
        public async Task<SKImage> OnCanvasViewPaintSurface(SKEventArgsToReturn skEventArgsToReturn, DrawContent drawContent)
        {
            SKImageInfo info = skEventArgsToReturn.Info;
            SKSurface surface = skEventArgsToReturn.Surface;
            SKCanvas canvas = surface.Canvas;

            SKPaint titlePaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                TextSize = 50,
                FakeBoldText = true,
                TextEncoding = SKTextEncoding.Utf32,
                Color = SKColors.DarkGreen
            };
            SKPaint textPaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                TextSize = 36,
                TextEncoding = SKTextEncoding.Utf32,
                Color = SKColors.Black
            };
            using (var stream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("selffont.ttf"))
            {
                titlePaint.Typeface = SKTypeface.FromStream(stream);

            };
            using (var stream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("selffont.ttf"))
            {
                textPaint.Typeface = SKTypeface.FromStream(stream);

            };

            canvas.Clear(SKColors.Snow);

            canvas.DrawText("食记", 48, 64, titlePaint);





            string painttext = _textAnalysisService.ConvertHtmlToText(drawContent.Content);
            for (int i = 0; i < painttext.Length / 16; i++)
            {
                var subString = painttext.Substring(i * 16, 16);
                canvas.DrawText(subString, 48, 148 + i * 64, textPaint);
            }

            var substring =
                painttext.Substring((painttext.Length / 16) * 16,
                    painttext.Length - (painttext.Length / 16) * 16);
            canvas.DrawText(substring, 48, 148 + (painttext.Length / 16) * 64, textPaint);
            if (drawContent.Pictures.Count == 1)
            {
                ImageSource retSource = null;
                byte[] value = drawContent.Pictures[0];
                SKBitmap bitmap = null;
                if (value != null)
                {
                    byte[] imageAsBytes = (byte[])value;
                    retSource = retSource = new MemoryStream(imageAsBytes);
                }
                bitmap = SKBitmap.Decode(retSource);
                float scale = Math.Min((float)info.Width / bitmap.Width,
                    (float)info.Height / bitmap.Height);
                float x = (info.Width - scale * bitmap.Width) / 2;
                float y = (info.Height - scale * bitmap.Height) / 2;
                SKRect srcmin = new SKRect(
                    0, 148 + 64 + (painttext.Length / 16) * 64,
                     (x + scale * bitmap.Width),
                     scale * bitmap.Height + 148 + 64 + (painttext.Length / 16) * 64);
                canvas.DrawBitmap(bitmap, srcmin);

            }
            else if (drawContent.Pictures.Count % 3 == 0)
            {
                for (int i = 0; i < drawContent.Pictures.Count; i++)
                {
                    ImageSource retSource = null;
                    byte[] value = drawContent.Pictures[i];
                    SKBitmap bitmap = null;
                    if (value != null)
                    {
                        byte[] imageAsBytes = (byte[])value;
                        retSource = retSource = new MemoryStream(imageAsBytes);
                    }

                    bitmap = SKBitmap.Decode(retSource);

                    float scale = Math.Min((float)info.Width / bitmap.Width,
                        (float)info.Height / bitmap.Height);
                    float x = (info.Width - scale * bitmap.Width) / 2;
                    float y = (info.Height - scale * bitmap.Height) / 2;
                    SKRect src = SKRect.Empty;
                    if (bitmap.Width > bitmap.Height)
                    {
                        src = new SKRect((bitmap.Width - bitmap.Height) / 2, 0,
                            bitmap.Width - (bitmap.Width - bitmap.Height) / 2,
                            bitmap.Height);
                    }
                    else if (bitmap.Width <= bitmap.Height)
                    {
                        src = new SKRect(0, (bitmap.Height - bitmap.Width) / 2,
                            bitmap.Width,
                            bitmap.Height - (bitmap.Height - bitmap.Width) / 2);
                    }

                    var localWidth = info.Width / 3;
                    SKRect des = new SKRect(localWidth * (i % 3),
                        148 + 64 + (painttext.Length / 16) * 64 + (i / 3) * localWidth,
                        localWidth + localWidth * (i % 3),
                        148 + 64 + localWidth + (painttext.Length / 16) * 64 + (i / 3) * localWidth);
                    canvas.DrawBitmap(bitmap, src, des);
                }
            }
            else if (drawContent.Pictures.Count % 2 == 0 && drawContent.Pictures.Count < 8)
            {
                for (int i = 0; i < drawContent.Pictures.Count; i++)
                {
                    ImageSource retSource = null;
                    byte[] value = drawContent.Pictures[i];
                    SKBitmap bitmap = null;
                    if (value != null)
                    {
                        byte[] imageAsBytes = (byte[])value;
                        retSource = retSource = new MemoryStream(imageAsBytes);
                    }
                    bitmap = SKBitmap.Decode(retSource);

                    float scale = Math.Min((float)info.Width / bitmap.Width,
                        (float)info.Height / bitmap.Height);
                    float x = (info.Width - scale * bitmap.Width) / 2;
                    float y = (info.Height - scale * bitmap.Height) / 2;
                    SKRect src = SKRect.Empty;
                    if (bitmap.Width > bitmap.Height)
                    {
                        src = new SKRect(
                            (bitmap.Width - bitmap.Height) / 2, 0,
                            bitmap.Width - (bitmap.Width - bitmap.Height) / 2,
                            bitmap.Height);
                    }
                    else if (bitmap.Width <= bitmap.Height)
                    {
                        src = new SKRect(
                            0, (bitmap.Height - bitmap.Width) / 2,
                            bitmap.Width,
                            bitmap.Height - (bitmap.Height - bitmap.Width) / 2);
                    }

                    var localWidth = info.Width / 2;
                    SKRect des = new SKRect(
                        localWidth * (i % 2), 148 + 64 + (painttext.Length / 16) * 64 + (i / 2) * localWidth, localWidth + localWidth * (i % 2), localWidth + 148 + 64 + (painttext.Length / 16) * 64 + (i / 2) * localWidth);
                    canvas.DrawBitmap(bitmap, src, des);
                }


            }
            else
            {
                for (int i = 0; i < drawContent.Pictures.Count; i++)
                {
                    ImageSource retSource = null;
                    byte[] value = drawContent.Pictures[i];
                    SKBitmap bitmap = null;
                    if (value != null)
                    {
                        byte[] imageAsBytes = (byte[])value;
                        retSource = retSource = new MemoryStream(imageAsBytes);
                    }

                    bitmap = SKBitmap.Decode(retSource);

                    float scale = Math.Min((float)info.Width / bitmap.Width,
                        (float)info.Height / bitmap.Height);
                    float x = (info.Width - scale * bitmap.Width) / 2;
                    float y = (info.Height - scale * bitmap.Height) / 2;
                    SKRect src = SKRect.Empty;
                    if (bitmap.Width > bitmap.Height)
                    {
                        src = new SKRect((bitmap.Width - bitmap.Height) / 2, 0,
                            bitmap.Width - (bitmap.Width - bitmap.Height) / 2,
                            bitmap.Height);
                    }
                    else if (bitmap.Width <= bitmap.Height)
                    {
                        src = new SKRect(0, (bitmap.Height - bitmap.Width) / 2,
                            bitmap.Width,
                            bitmap.Height - (bitmap.Height - bitmap.Width) / 2);
                    }

                    var localWidth = info.Width / 3;
                    SKRect des = new SKRect(localWidth * (i % 3),
                        148 + 64 + (painttext.Length / 16) * 64 + (i / 3) * localWidth,
                        localWidth + localWidth * (i % 3),
                        148 + 64 + localWidth + (painttext.Length / 16) * 64 + (i / 3) * localWidth);
                    canvas.DrawBitmap(bitmap, src, des);
                }
            }

            return surface.Snapshot();
        }

        public void SavePic()
        {

        }
    }
}
